package com.me.components {
	import flash.events.MouseEvent;

	import spark.core.SpriteVisualElement;

	public class Grid45 extends SpriteVisualElement {
		public var walk:Boolean = false;
		public var row:int;
		public var col:int;
		private var _handler:Function;

		public function Grid45(w:int, h:int, _clickHandler:Function) {
			super();
			this.width = w;
			this.height = h;
			_handler = _clickHandler;
			drawWalk();
			addEventListener(MouseEvent.CLICK, clickHandler);
		}

		private function clickHandler(e:MouseEvent):void {
			if (walk) {
				drawNoWalk()
			} else {
				drawWalk();
			}
			_handler(this);
		}

		private var lineColor:uint = 0xcccccc;

		private var lineAlpha:Number = 1;

		private function draw(color:uint):void {
			drawGrid();
			graphics.beginFill(color, 0.7);
			graphics.drawCircle(0, 0, 10);
			graphics.endFill();
		}

		private function drawGrid():void {
			graphics.clear();

			graphics.beginFill(0xffffff, 0);
			graphics.drawRect(0, 0, width, height);
			graphics.endFill();

			graphics.moveTo(0 - width / 2, 0);
			graphics.lineStyle(1, lineColor, lineAlpha);
			graphics.lineTo(0, 0 - height / 2);
			graphics.moveTo(0 - width / 2, 0);
			graphics.lineStyle(1, lineColor, lineAlpha);
			graphics.lineTo(0, 0 + height / 2);
			graphics.moveTo(0 + width / 2, 0);
			graphics.lineStyle(1, lineColor, lineAlpha);
			graphics.lineTo(0, 0 - height / 2);
			graphics.moveTo(0 + width / 2, 0);
			graphics.lineStyle(1, lineColor, lineAlpha);
			graphics.lineTo(0, 0 + height / 2);
		}

		public function drawWalk():void {
			graphics.clear();
			drawGrid();
			walk = true;
		}

		public function drawNoWalk():void {
			draw(0xff0000);
			walk = false;
		}
	}
}
